//
//  ViewController.swift
//  LiveShow
//
//  Created by dai zhong on 2017/4/17.
//  Copyright © 2017年 company.cn. All rights reserved.
//

import UIKit

let kPagedCollectionCellIdentifier = "kPagedCollectionCellIdentifier";

class TestPagedViewController: UIViewController {

    fileprivate lazy var pagedCollectionView :PagedCollectionView = { () -> PagedCollectionView in
    
        let frame = CGRect(x: 0, y: 100, width: self.view.bounds.width, height: 300);
        var pagedCollectionStyle = PagedCollectionStyle();
        pagedCollectionStyle.aPageColumnCount = 4;
        pagedCollectionStyle.aPageRowCout = 3;
        pagedCollectionStyle.lineSpacing = 5.0;
        pagedCollectionStyle.interitemSpacing = 5.0;
        pagedCollectionStyle.edgInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15);
        
        let pagedCollectionView :PagedCollectionView = PagedCollectionView(pagedCollectionFrame: frame, pagedCollectionStyle: pagedCollectionStyle);
        //pagedCollectionView.delegate = self;
        pagedCollectionView.dataSource = self;
        pagedCollectionView.registerCell(UICollectionViewCell.self, forCellWithReuseIdentifier: kPagedCollectionCellIdentifier)
        return pagedCollectionView;
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI();
    }
}

extension TestPagedViewController{

    fileprivate func setupUI(){
        
        self.view.addSubview(pagedCollectionView);
        self.view.backgroundColor = UIColor.gray;
    }
}

// MARK:- Event Method
extension TestPagedViewController{

    /***
     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     
     pagedCollectionView.selectItemAtPath(didSelectItemAt: NSIndexPath(item: 6, section: 1) as IndexPath, animated: true);
     }
     
     ***/
}

// MARK:- PagedCollectionViewDataSource
extension TestPagedViewController:PagedCollectionViewDataSource{

    func numberOfSections(in collectionView: PagedCollectionView) -> Int {
        return 2;
    }
    
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 23;
    }
    
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = pagedCollectionView.dequeueReusableCell(withReuseIdentifier: kPagedCollectionCellIdentifier, for: indexPath);
        
        var isExist = false;
        for childView in cell.contentView.subviews {
            if childView.tag == 1000 && childView.isKind(of: UILabel.self) {
                (childView as! UILabel).text = "s=\(indexPath.section) r=\(indexPath.row)";
                isExist = true;
            }
        }
        if !isExist {
            let label = UILabel();
            label.frame = CGRect(x: 0, y: (cell.contentView.bounds.height - 20)*0.5, width: cell.contentView.bounds.width, height: 20)
            label.text = "s=\(indexPath.section) r=\(indexPath.row)";
            label.tag = 1000;
            cell.contentView .addSubview(label);
        }
        
        cell.backgroundColor = UIColor.blue;
        return cell;
    }
}

/***
 // MARK:- PagedCollectionViewDelegate
 extension TestPagedViewController:PagedCollectionViewDelegate{
 
 func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didSelectItemAt indexPath: IndexPath) {
 
 print("didSelectItemAt indexPath = \(indexPath)");
 }
 
 func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didScrolledPageAt indexPath: IndexPath) {
 print("didScrolledPageAt indexPath = \(indexPath)");
 }
 
 func pagedCollectionView(_ pagedCollectionView: PagedCollectionView) {
 print(pagedCollectionView.contentOffset?.y ?? 0)
 }
 }
 
 **/






















